package com.penjin.android.activity.secretary;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import com.easemob.util.NetUtils;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.penjin.android.R;
import com.penjin.android.adapter.ZhangLiangListAdapter;
import com.penjin.android.constants.HttpConstants;
import com.penjin.android.domain.BaseNews;
import com.penjin.android.domain.News;
import com.penjin.android.domain.PenjinUser;
import com.penjin.android.http.HttpService;
import com.penjin.android.service.UserService;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lxl on 2016/4/9.
 */
public class SecretaryNewsListActivity extends Activity{

    private ZhangLiangListAdapter adapter ;

    private ListView listView ;

    private List<News> datas = new ArrayList<News>() ;

    private Context context = this ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.layout_activity_zhangliang);

        listView = (ListView) findViewById(R.id.activity_zhangliang_list);

        init();

        adapter = new ZhangLiangListAdapter(this,datas,HttpConstants.CompanyPushDetail) ;

        listView.setAdapter(adapter);

    }

    public void init(){

        if( datas.isEmpty()){

            HttpService httpService = HttpService.getInstance(this);
            //�ж������Ƿ���
            if (NetUtils.hasNetwork(this)) {
                try {

                    RequestParams requestParams = new RequestParams();
                    PenjinUser user =  UserService.getInstance(this).getCurrentUser() ;

                    requestParams.put("companyId", user.getCompanyId());
                    requestParams.put("userName", user.getPhone());
                    httpService.postRequest(this, HttpConstants.HOST + HttpConstants.CompanyPush, requestParams, new TextHttpResponseHandler() {
                        @Override
                        public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                            if (s == null) {
                                Toast.makeText(context, "�������ڲ�����", Toast.LENGTH_SHORT).show();
                            } else {
                                System.out.println(s);
                                Toast.makeText(context, "��Դ·������", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onSuccess(int i, Header[] headers, String s) {
                            try {

                                Log.d("JPush",s) ;

                                JSONObject jo = new JSONObject(s);
                                if (jo.getBoolean("result")) {
                                    JSONArray ja = jo.getJSONArray("data");

                                    Log.d("JPush",ja.toString()) ;

                                    if (ja != null && ja.length() > 0) {

                                        int k = 0 ;

                                        News news = new News() ;

                                        for( int ij = 0 ; ij < ja.length() ; ij++){

                                            JSONObject jsonObject = ja.getJSONObject(ij) ;

                                            if( k == 0 ){

                                                news = new News() ;

                                            }

                                            Log.d("JPush",jsonObject.toString()) ;

                                            BaseNews data = new BaseNews() ;

                                            data.setImage(jsonObject.getString("picture"));
                                            data.setId(jsonObject.getString("id"));
                                            data.setTitle(jsonObject.getString("title"));

                                            news.addNews(data);

                                            k++ ;
                                            if( k == 5 || ij == ja.length() - 1 ){

                                                datas.add(news) ;
                                                k = 0 ;

                                            }

                                        }

                                        Log.d("JPush",String.valueOf(datas.size())) ;
                                        adapter.notifyDataSetChanged();

                                    } else {
                                        Toast.makeText(context, "���������Ϣ", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                Toast.makeText(this, "���粻����...", Toast.LENGTH_SHORT).show();
            }
        }



    }




}
